package com.zhengke.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhengke.common.util.FastDfsApiOpr;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zhengke.common.util.DateUtil;
import com.zhengke.common.util.LoadProperties;
import com.zhengke.pojo.BaleGoods;
import com.zhengke.pojo.ReturnAjax;
import com.zhengke.pojo.TansferReturn;
import com.zhengke.pojo.Result;
import com.zhengke.service.BaleGoodsService;

/**
 * 创建时间：2019年12月26日 下午3:13:58
 * <p>
 * 项目名称：tvmall
 *
 * @author 刘浩
 * @version 1.0
 * <p>
 * 文件名称：BaleGoodsController.java
 * <p>
 * 类说明：  打包商品管理控制层
 */
@Controller
@RequestMapping("BaleGoods")
public class BaleGoodsController {
    @Autowired
    BaleGoodsService bgs;

    /**
     * 跳转到打包商品管理页面
     *
     * @return
     */
    @RequestMapping("goBaleGoods.do")
    public String goBaleGoods() {
        return "heart/BaleGoods";
    }

    /**
     * 跳转到增加打包商品管理页面
     *
     * @return
     */
    @RequestMapping("goBaleGoods-add.do")
    public String goBaleGoodsAdd() {
        return "heart/BaleGoods-add";
    }

    /**
     * 跳转到增加打包商品管理页面
     *
     * @return
     */
    @RequestMapping("goBaleGoods-addcp.do")
    public String goBaleGoodsAddcp() {
        return "heart/BaleGoods-addcp";
    }

    /**
     * 查询打包商品
     *
     * @param sth
     * @return
     */
    @RequestMapping("selectBaleGoods.do")
    @ResponseBody
    public Result selectBaleGoods(BaleGoods sth) {
        Result rs = bgs.selectBaleGoods(sth);
        return rs;
    }

    /**
     * 增加打包商品
     *
     * @param sth
     * @return
     */
    @RequestMapping("addBaleGoods.do")
    @ResponseBody
    public String addBaleGoods(@RequestBody BaleGoods sth) {
        String baleGoodsId = bgs.addBaleGoods(sth);

        return baleGoodsId;
    }

    /**
     * 上传图片
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("UploadPictures.do")
    public Object UploadPictures(@RequestParam CommonsMultipartFile file) {

        try {
            String imageFileName = file.getOriginalFilename();
            String extName = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            if (file != null && !file.isEmpty()) {
                String path = LoadProperties.findValue("imgPath") + "/" + DateUtil.getSystemDateTimeByFormat("yyyyMMddHHmmss") + "." + extName;
                File file1 = new File(path);
                if (!file1.getParentFile().exists()) {
                    file1.getParentFile().mkdirs();
                }
                FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
                //上传fastDFS后图片完整地址
                String file_id = FastDfsApiOpr.upload(file1.getPath(), extName);
                //读取config.properties查找访问路径的ip+端口
                String rtnPath = LoadProperties.findValue("FILE_SERVER_URL") + file_id;
                //String rtnPath = LoadProperties.findValue("viewUrl")+"?path="+file1.getPath();
                if (file1 != null) {
                    file1.delete();
                }
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("src", rtnPath);
                ReturnAjax ra = new ReturnAjax();
                ra.setData(data);
                ra.setMsg("成功");
                ra.setCode("0");
                return ra;
            } else {
                return new ReturnAjax("300", "文件格式不正确，请上传 *.png,*.jpeg,*.gif,*.jpg类型图片");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnAjax("300", "上传失败");
    }

    /**
     * 到修改商品的页面
     *
     * @param baleGoodsId
     * @param model
     * @return
     */
    @RequestMapping("goModify.do")
    public String goModify(String baleGoodsId, ModelMap model) {
        System.out.println(baleGoodsId);
        BaleGoods sth = new BaleGoods();
        sth.setBaleGoodsId(Integer.parseInt(baleGoodsId));
        Result rs = bgs.selectBaleGoods(sth);
        List<?> data = rs.getData();
        BaleGoods selectBaleGoods = (BaleGoods) data.get(0);
        Date baleGoodValidity = selectBaleGoods.getBaleGoodValidity();

        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");

        try {
            if (baleGoodValidity != null) {
                String format = dateFm.format(baleGoodValidity);
                model.addAttribute("BaleGoods", selectBaleGoods);
                //读取config.properties查找访问路径的ip+端口
                String fileServerUrl = LoadProperties.findValue("FILE_SERVER_URL");
                model.put("date", format);
                model.put("fileServerUrl", fileServerUrl);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //model.put("BaleGoods", selectBaleGoods);
        return "heart/BaleGoods-modify";
    }

    /**
     * 修改打包商品
     *
     * @param bg
     * @return
     */
    @ResponseBody
    @RequestMapping("modifyBaleGoods.do")
    public Integer modifyBaleGoods(@RequestBody BaleGoods bg) {

        //读取config.properties查找访问路径的ip+端口
        String fileServerUrl = null;
        String picpath = bg.getPicpath();
        String fileId = bg.getThumbPicpath();
        try {
            fileServerUrl = LoadProperties.findValue("FILE_SERVER_URL");
            picpath = picpath.replace(fileServerUrl, "");
            bg.setPicpath(picpath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int modifyBaleGoods = bgs.modifyBaleGoods(bg);
        fileId = fileId.replace(fileServerUrl, "");
        if (modifyBaleGoods > 0) {
            if (!fileId.equals(picpath))
                FastDfsApiOpr.deleteFile(fileId);
        }
        return modifyBaleGoods;
    }

    /**
     * 批量删除打包商品
     *
     * @param baleGoodsId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteBaleGoods.do")
    public Integer deleteBaleGoods(@RequestBody String baleGoodsId) {
        int deleteBaleGoods = bgs.deleteBaleGoods(baleGoodsId);
        return deleteBaleGoods;
    }

    /**
     * 删除打包商品
     *
     * @param baleGoodsId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteBaleGoodsOne.do")
    public Integer deleteBaleGoods(Integer baleGoodsId) {
        int deleteBaleGoods = bgs.deleteBaleGoodsOne(baleGoodsId);
        return deleteBaleGoods;
    }

    /**
     * 查询 可打包的商品
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("selectCommonGoods.do")
    public List<TansferReturn> selectCommonGoods() {
        List<TansferReturn> selectCommonGoodsList = bgs.selectCommonGoods();
        return selectCommonGoodsList;
    }
}
